home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3340 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.nstn.ca!news
  2. From: keichele@ac.dal.ca (Klaus Eichele)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: command line argument help
  5. Date: Sun, 28 Jan 1996 05:43:36 GMT
  6. Organization: Dalhousie University
  7. Message-ID: <4eekdi$9d@news.nstn.ca>
  8. References: <4edfth$ok@muss.CIS.McMaster.CA> <4ee3js$h57@umbc9.umbc.edu>
  9. NNTP-Posting-Host: rewasylishen.chem.dal.ca
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. schlein@umbc.edu (Jonas J. Schlein) wrote:
  13.  
  14. >In article <4edfth$ok@muss.CIS.McMaster.CA>,  <shadowfax> wrote:
  15. >|> to all the c gods out there:
  16.  
  17. >I don't see any, but I'll try to help anyway...
  18.  
  19. >|> i am not having too much success with command line arguments.  i am 
  20. >|> trying to make a simple sumation executable for dos.  i am using borland 
  21. >|> c++ v3.1.  what i want as the end result is the user just types:
  22. >|> 
  23. >|> c:\> sum 6 3
  24. >|> 
  25. >|> and the executable would output an answer of 9. the following is my program:
  26. >|> 
  27. >|> #include <stdio.h>
  28. >|> #include <stdlib.h>
  29. >|> 
  30. >|> int main(char *argv[])
  31.  
  32. >Well there ya go! This is an illegal definition of main(). Instead use
  33. >int main (int argc, char *argv[]).
  34.  
  35. >|> {
  36. >|>    int iSum = 0;
  37. >|> 
  38. >|>    iSum = atoi(argv[1]) + atoi(argv[2]);
  39. >|>    printf("\nthe answer is %d", iSum);
  40.  
  41. >You may want to end the printf() with a '\n' as well.
  42.  
  43. >|>    return(0);
  44.  
  45. >Good going...You get bonus points!
  46.  
  47. >|> }
  48. >|> 
  49. >|> when i compile and run, it always outputs an anser of 0.  can anyone tell 
  50. >|> me what's wrong?
  51.  
  52. >Already did...Make the change to the definition of main() and you'll be good
  53. >to go!
  54. >-- 
  55. >"If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  56.  
  57. >Jonas J. Schlein  (schlein@gl.umbc.edu)
  58.  
  59. Maybe one should add that in such instances it is highly advisable to
  60. check the value of argc itself before doing any processing (e.g., to
  61. display a message showing proper use/syntax of the program in case the
  62. wrong number of arguments has been supplied).
  63.  
  64. Good Luck,
  65. Klaus
  66. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  67. Klaus Eichele         keichele@ac.dal.ca  
  68. http://ac.dal.ca/~keichele/keichele.html
  69.  
  70.